package com.uyaer.engine.core
{
	import com.uyaer.engine.display.el.DynamicSprite;
	import com.uyaer.engine.display.el.StaticSprite;
	import com.uyaer.engine.loader.LoaderManager;
	import com.uyaer.engine.loader.MyLoaderInfo;
	import com.uyaer.engine.loader.URI;
	import com.uyaer.engine.text.TextCache;
	import com.uyaer.engine.utils.HashMap;
	import com.uyaer.engine.utils.Utils;
	import com.uyaer.engine.vo.GameObject;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class Model extends Sprite implements IModel
	{
		//物体类型为静态，不可以进行移动
		public static const STATIC_NO:int = -2;
		public static const STATIC_COLLISION:int = -1;
		public static const DYNAMIC_NO:int = 1;
		public static const DYNAMIC_COLLISION:int = 2;
		//人物
		public static const NONE:int = 0;
		public static const NPC:int = 1;
		public static const PLAYER_MINE:int = 2; //我自己
		public static const PLAYER_OTHER:int = 3;  //队友或中立
		public static const PLAYER_EMERY:int = 4;  //敌人
		
		//角色视图改变
		protected static const STAND_VIEW:String = "stand";
		protected static const MOVE_VIEW:String = "move";
		protected static const RUN_VIEW:String = "run";
		protected static const JUMP_VIEW:String = "jump";
		protected static const FIND_VIEW:String = "find";
		protected static const ATTACK_VIEW:String = "attack";
		protected static const SKILL_VIEW:String = "skill";
		
		protected var viewType:int;  //视图是否动态
		protected var type:int;  //人物类型
		
		public var bean:GameObject; //数据源
		public var z_axis:Number;  //轴，一般为y值，但是跳跃或者飞行时，z不变
		
		protected var shadeSp:Sprite; //阴影
		protected var roleView:Sprite;
		protected var viewSp:Sprite; //物品的显示对象
		protected var nameSp:Sprite;
		protected var nameTF:TextField; //名称
		protected var bloodSp:Sprite; //血量
		
		private var staticView:StaticSprite;
		private var dynamicView:DynamicSprite;
		
//		private var statesMap:HashMap = new HashMap();
		private static var resBmdDataMap:HashMap = new HashMap(); //使用一份图像数据
		
		public function Model(go:GameObject)
		{
			this.bean = go;
			this.type = go.pType;
			this.viewType = go.viewType;
			
			resLoad();
			
			shadeSp = new Sprite();
			roleView = new Sprite();
			viewSp = new Sprite();
			nameSp = new Sprite();
			nameTF = new TextField();
			nameTF.autoSize = TextFieldAutoSize.CENTER;
			nameSp.addChild(nameTF);
			bloodSp = new Sprite();
			addChild(shadeSp);
			addChild(roleView);
			roleView.addChild(viewSp);
			roleView.addChild(nameSp);
			roleView.addChild(bloodSp);
		}
		
		private function resLoad():void{
			if(!resBmdDataMap.hasKey(bean.resName)){
				LoaderManager.instance.add(new MyLoaderInfo(URI.makeGameObjectURI(bean.resName,"png"),_resLoadOver ,8));
			}else{
				changeStateView(bean.defaultState);
			}
		}
		
		private function _resLoadOver(display:DisplayObject):void{
			resBmdDataMap.put(bean.resName , (display as Bitmap).bitmapData.clone());
			(display as Bitmap).bitmapData.dispose();
			LoaderManager.instance.loadBinary(new MyLoaderInfo(URI.makeGameObjectURI(bean.resName,"json"),_jsonLoadOver,10));
		}
		
		private function _jsonLoadOver(data:String,odata:*):void{
			TextCache.texturePacker.putAll(Utils.texturePacker(data));
			changeStateView(bean.defaultState);
		}
		
		public function changeName(name:String):void{
			nameTF.text = name;
		}
		
		/**
		 *更新布局状态 
		 * 
		 */		
		public function updateLayout():void{
			nameSp.y = -viewSp.height - 15;
			nameSp.x = -nameSp.width * 0.5;
		}
		
		/**
		 *改变人物状态 
		 * 
		 */		
		public function changeStateView(state:String):void{
			state = bean.id +"_"+ state;
			var bmd:BitmapData = resBmdDataMap.get(bean.resName) as BitmapData;
			if(viewType>0){
//				dynamicView && dynamicView.destory();
				dynamicView = new DynamicSprite(state,bean.totalFrames, bmd);
				viewSp.addChild(dynamicView);
			}else{
				staticView = new StaticSprite(state , bmd);
				viewSp.addChild(staticView);
			}
			updateLayout();
		}
		
		public function tick():void{
			dynamicView && dynamicView.render();
			staticView && staticView.render();
		}
	}
}